
Ext.define('CDR.widget.PBXAssign', {
    extend: 'Ext.form.Panel',
    alias : 'widget.pbxassign',
	buttonAlign:'center',
	border: false,
	title: CDR.I18N['organization.assign-pbx.form.title'],
	layout:'anchor',
	bodyPadding: 10,
	treeView: null,
	fieldDefaults: {
		msgTarget: 'side',
		labelWidth: 65
	},
	initComponent: function() {
		var me = this;
		me.callParent();
	   this.itemSelector = Ext.createWidget('itemselector', {
	        name: 'itemselector',
	        anchor: '100%',
	        store: Ext.create('Ext.data.ArrayStore', {
	            fields: ['obId','name'],
	        }),
	        displayField: 'name',
	        valueField: 'obId',
	        border: false,
	        allowBlank: false,
	        msgTarget: 'side'
	    });
	   
	   this.assignTo = Ext.createWidget('combo', {
			fieldLabel:CDR.I18N['organization.assign-pbx.form.assign-to'],
			anchor:'20%',
			labelWidth:75,
			mode:  'local',
			triggerAction:  'all',
			forceSelection: true,
			editable: false,
			name: 'assignTo',
			displayField: 'name',
			allowBlank: false,
			valueField: 'obId',
			queryMode: 'local',
			store: Ext.create('Ext.data.Store', {
				fields : ['name', 'obId']
			})
		});
	   this.add(this.itemSelector);
	   this.add(this.assignTo);
	},
	editData: function(data, tree){
		if(data) {
			this.itemSelector.bindStore(Ext.create('Ext.data.Store', {
	            fields: ['obId', 'name'],
	            data: data.result.pbxs
	        }));
			this.assignTo.store.loadData(data.result.sites);
			if(tree){
				var node = tree.getSelectedNode();
				if(node) {
					this.assignTo.setValue(node.getId());
				}
			}
		}
	},
	buttons:[
		{
			text: CDR.I18N['button.save'],
			iconCls: 'icon-save',
			handler: function() {
				var pbxForm = this.up('pbxassign');
				var form = this.up('form').getForm();
				if (form.isValid()) {
					form.submit({
						success: function(form, action) {
						   Ext.Msg.alert('Success', action.result.msg);
						   if(pbxForm.treeView){
							  	var nodeValue = pbxForm.assignTo.getValue(); 
								pbxForm.treeView.reloadSameItemClick(nodeValue);
						   }
						},
						failure: function(form, action) {
							Ext.Msg.alert('Failed', action.result.msg);
						}
					});
				}
			}
		},{	
			text: CDR.I18N['button.reset'],
			handler: function () {
				this.up('form').getForm().reset();
			}
		}
	]
});

Ext.define('CDR.widget.PBXForm', {
    extend: 'Ext.form.Panel',
    alias : 'widget.pbxform',
	buttonAlign:'center',
	border: false,
	title: CDR.I18N['pbx.form.title'],
	bodyStyle:'padding:5px 5px 0',
	addFieldValues : [],
	type: CDR.Constant.WINDOW_NEW_MODE,
	targetObject: null,
	fieldDefaults: {
		msgTarget: 'side',
		labelWidth: 65
	},
	initComponent: function() {
		var me = this;
		me.callParent();
		var addFields = this.initialConfig.addFields;
		if(addFields){
			Ext.each(addFields, function (item){
				this.add(item);
			}, me);
		}
	},
	
	initData: function(data){
		this.type = CDR.Constant.WINDOW_NEW_MODE;
	},
	editData: function(data){
		this.type = CDR.Constant.WINDOW_EDIT_MODE;
		if(data) {
			if(data.result) {
				var pbxNo = this.down('textfield[name=pbxNo]');
				pbxNo.setValue(data.result.pbxNo);
				var name = this.down('textfield[name=name]');
				name.setValue(data.result.name);
				var description = this.down('textfield[name=description]');
				description.setValue(data.result.description);
			}
		}
	},
	items: [{
		xtype: 'container',
		anchor: '100%',
		layout:'column',
		items: [{
			xtype:'container',
			columnWidth:.25,
			layout: 'anchor',
			items: [{
				xtype:'textfield',
				fieldLabel: 'Pbx No',
				name:'pbxNo',
				anchor:'96%',
				labelWidth: 75
			}]
		},{
			xtype:'container',
			columnWidth:.25,
			layout: 'anchor',
			items: [{
				xtype:'textfield',
				fieldLabel: CDR.I18N['pbx.form.name'],
				name:'name',
				allowBlank: false,
				anchor:'96%',
				labelWidth: 50,
				listeners:{
					render: function(){
						var me =  this.up('panel');
						Ext.createWidget('checknameValidator',{targetObject: this, checkOn: 'PBX', listenerObject:{target: me, event:'added'}});
					}
				}
			}]
		}, {
			xtype:'container',
			columnWidth:.55,
			layout: 'anchor',
			items: [{
				xtype:'textareafield',
				fieldLabel: CDR.I18N['pbx.form.description'],
				name:'description',
				anchor:'96%',
				labelWidth: 75
			}]
		}]
	}]
	,
	buttons:[
		{
			text: CDR.I18N['button.save'],
			iconCls: 'icon-save',
			formBind: true,
			handler: function() {
				var pbxForm = this.up('form');
				var form = this.up('form').getForm();
				if(pbxForm.addFieldValues.length > 0){
					Ext.each(pbxForm.addFieldValues, function(i){
						var hf = this.down('hidden[name=' + i.name +']');
						if(hf) {
							hf.setValue(i.value);
						}
					},pbxForm);
				}
				if (form.isValid()) {
					form.submit({
						success: function(form, action) {
						   Ext.Msg.alert('Success', action.result.msg);
						   form.reset();
						   if(pbxForm.targetObject){
								pbxForm.targetObject.submitOk(action.result, pbxForm.type);
						   }
						},
						failure: function(form, action) {
							Ext.Msg.alert('Failed', action.result.msg);
						}
					});
				}
			}
		},{	
			text: CDR.I18N['button.reset'],
			handler: function () {
				this.up('form').getForm().reset();
			}
		}
	]
});